const { Server } = require('socket.io')

const socketio = {}

socketio.getSocket = function (server) {
    var io = new Server(server, { cors: true })

    //保存连接上的用户socket.io.id和用户id
    io.sockets.on("connection", (socket) => {

        socket.on("joinRoom", ({ room }) => {
            socket.join(room);
            console.log(`User ${socket.id} joined room ${room}`);
        });

        //退出后断开连接
        socket.on('disconnect', (reason) => {
            console.log("disconnect reason ", reason)
        })

        socket.on("message", async (data) => {
            //获取聊天记录存放到数据库

        })
    })
}

module.exports = socketio